/*
 get参数处理
*/
const url = require('url')

// let str = 'http://www.baidu.com/abc?flag=123&keyword=java'

// let ret = url.parse(str)
// console.log(ret)
// console.log(ret.query)
// let ret1 = url.parse(str, true) //第二个参数设置成true可以将query值设置成对象
// console.log(ret1)
// console.log(ret1.query)

// {
//   protocol: 'http:',
//   slashes: true,
//   auth: null,
//   host: 'www.baidu.com',
//   port: null,
//   hostname: 'www.baidu.com',
//   hash: null,
//   search: '?flag=123&keyword=java',
//   query: { flag: '123', keyword: 'java' },
//   pathname: '/abc',
//   path: '/abc?flag=123&keyword=java',
//   href: 'http://www.baidu.com/abc?flag=123&keyword=java' }

// let obj = {
//   protocol: 'http:',
//   slashes: true,
//   auth: null,
//   host: 'www.baidu.com',
//   port: null,
//   hostname: 'www.baidu.com',
//   hash: null,
//   search: '?flag=123&keyword=java',
//   query: { flag: '123', keyword: 'java' },
//   pathname: '/abc',
//   path: '/abc?flag=123&keyword=java',
//   href: 'http://www.baidu.com/abc?flag=123&keyword=java' }
//   let ret2 = url.format(obj)
//   console.log(ret2) //http://www.baidu.com/abc?flag=123&keyword=java


// const http = require('http')
// const path = require('path')
// const fs = require('fs')
// //获取get请求的参数
// http.createServer((req, res) => {
// 	let obj = url.parse(req.url, true)
// 	res.writeHead(200, {
// 		'Content-Type': "text/plain; charset=utf8"
// 	})
// 	res.end(obj.query.username + '===========' + obj.query.password)
// }).listen(3000, () => {
// 	console.log('running...')
// })



/*
  POST参数获取
*/
const querystring = require('querystring')
// let param = 'username=李四&password=123'
// let obj = querystring.parse(param) //字符串成对象
// console.log(obj)

//stringify的作用就是把对象转成字符串
// let obj1 = {
// 	flag: '123',
// 	abc: ['hello', 'hi']
// }
// let str1 = querystring.stringify(obj1)
// console.log(str1)



const http = require('http')

//postmain中的数据类型由form-data改成x-www-form-urlencoded
http.createServer((req, res) => {
	if (req.url.startsWith('/login')) {
    let pdata = ''
	  req.on('data', (chunk) => {
	    pdata += chunk
	  })
	  req.on('end', () => {
	  	console.log(pdata)
	  	let obj = querystring.parse(pdata)
	  	console.log(obj)
	  	res.end(obj.username + '-------' + obj.password)
	  })
	}
}).listen(3000, () => {
	console.log('running...')
})